const {Movie, Music, Sentence} = require('./classicModel')
const {Favor} = require('./favorModel')
// 扁平化方法
const {flatten} = require('lodash')
class Art {

  constructor(art_id,type) {
    this.art_id = art_id;
    this.type = type
  }
  
  async getDetail(uid) {
    const art = await Art.getData(this.art_id, this.type)
    if(!art) {
      throw new global.errs.NotFound()
    }
    const like = await Favor.userLikeIt(this.art_id, this.type, uid)
    // art.setDataValue('like_status', like)
    return {
      art,
      like_status: like
    }
  }
  
  static async getList(artInfoList) {
    // in 
    // [ids]
    const artInfoObj = {
      100: [],
      200: [],
      300: []
    }
    for(let artInfo of artInfoList) {
      // for in for of
      artInfoObj[artInfo.type].push(artInfo.art_id)
    }
    const arts = []
    for(let key in artInfoObj) {
      const ids = artInfoObj[key]
      if(!ids) {
        continue
      }
      key = parseInt(key)
      arts.push(await Art._getListByType(artInfoObj[key], key))
    }
    // 二维数组
    return flatten(arts)
  }
  
  // 私有方法
  static async _getListByType(ids, type) {
    let art = []
    const finder = {
      where: {
        id: {
          [Op.in]: ids
        }
      }
    }
    // const scope = useScope ? 'bh': null
    switch (type) {
      case 100:
        art = await Movie.findOne(finder)
        break;
      case 200:
        art = await Music.findOne(finder)
        break;
      case 300:
        art = await Sentence.findOne(finder)
        break;
      case 400:
        
        break;
      default:
        break;
    }
    return art;
  }

  static async getData(art_id, type) {
    let art = null;
    const finder = {
      where: {
        id: art_id
      }
    }
    switch (type) {
      case 100:
        art = await Movie.findOne(finder)
        break;
      case 200:
        art = await Music.findOne(finder)
        break;
      case 300:
        art = await Sentence.findOne(finder)
        break;
      case 400:
        
        break;
    
      default:
        break;
    }
    return art;
  }


}

module.exports = {
  Art
}